package Client.GUI;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import Client.logic.ClientIF;
import Server.DataBase.Course;
import Server.DataBase.Lesson;
import Server.DataBase.Student;
import Server.Message.MessageGetAllStudents;
import Server.Message.MessageGetCoursesReplay;
import Server.Message.MessageGetLessonByCourse;
import Server.Message.MessageGetStudentsReplay;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageSendRequest;
import Server.Message.MessageUnassignCourse;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class AssignStudentToCoursePanel extends MyJPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Student> allStudentArray =null;
	private ArrayList<Course> courseArray =null;
	private ArrayList<Lesson> lessonArray =null;
	private JComboBox studentcomboBox;
	private JComboBox coursecomboBox;
	private JComboBox lessoncomboBox;
	private JLabel lblStudent;
	private JLabel lblCourse;
	private JLabel lblLesson;
	private JButton btnAssign;
	private JButton btnReset;
	private Student student;
	private Course course;
	private Lesson lesson;
	
	public AssignStudentToCoursePanel( ClientIF client) {
		super(PanelType.ASSIGN_STUDENT_TO_COURSE_PANEL, client);
		setLayout(null);	
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"assign student to course.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(25, 13, 528, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		init();
	}
	
	public void initArrays(){
		allStudentArray = new ArrayList<Student>();
		getClient().sendMsgToServer(new MessageGetAllStudents());
		MessageGetStudentsReplay rep = (MessageGetStudentsReplay) getClient().getMessageFromServer();
		allStudentArray = rep.getArray();
		
	}
	public void initComboBoxs(){
		
		studentcomboBox = new JComboBox();
		studentcomboBox.setBounds(211, 128, 129, 22);
		add(studentcomboBox);
		
		coursecomboBox = new JComboBox();
		coursecomboBox.setBounds(211, 199, 129, 22);
		coursecomboBox.setEnabled(false);
		add(coursecomboBox);
		
		lessoncomboBox = new JComboBox();
		lessoncomboBox.setBounds(211, 276, 129, 22);
		lessoncomboBox.setEnabled(false);
		add(lessoncomboBox);
	}
	
	public void initLabels(){
		
		lblStudent = new JLabel("student:");
		lblStudent.setBounds(102, 131, 56, 16);
		add(lblStudent);
		
		lblCourse = new JLabel("course:");
		lblCourse.setBounds(102, 202, 56, 16);
		add(lblCourse);
		
		lblLesson = new JLabel("lesson:");
		lblLesson.setBounds(102, 279, 56, 16);
		add(lblLesson);
	}
	
	public void init(){
		initArrays();
		initComboBoxs();
		initLabels();
		btnAssign = new JButton("Send request to manager");
		btnAssign.setBounds(191, 429, 219, 25);
		btnAssign.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().sendMsgToServer(new MessageSendRequest(1, student.getIdUser(), lesson.getIdLesson()));
				JOptionPane.showMessageDialog((Component) getClient(), "Request has been send");
				getClient().swapFromBack(pushPanel());
			}
		});
		add(btnAssign);
		btnAssign.setEnabled(false);
		
		btnReset = new JButton("reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().swapFromBack(pushPanel());
			}
		});
		btnReset.setBounds(211, 343, 97, 25);
		add(btnReset);
		btnReset.setEnabled(false);
		
		for (int i=0; i<=allStudentArray.size(); i++){
			if (i==0) studentcomboBox.addItem("Choose..");
			else studentcomboBox.addItem(allStudentArray.get(i-1));
		}
		
		studentcomboBox.addActionListener(new StudentListener());
		}
	
	private class StudentListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				student = (Student) studentcomboBox.getSelectedItem();
			
				getClient().sendMsgToServer(new MessageUnassignCourse(student.getIdUser()));
				MessageGetCoursesReplay msg2 = (MessageGetCoursesReplay) getClient().getMessageFromServer();
				courseArray = msg2.getCourseArray();
			
				for (int i=0; i<=courseArray.size(); i++){
					if (i==0) coursecomboBox.addItem("Choose..");
					else coursecomboBox.addItem(courseArray.get(i-1));
				}
				coursecomboBox.setEnabled(true);
				studentcomboBox.setEnabled(false);
				btnReset.setEnabled(true);
				coursecomboBox.addActionListener(new CourseListener());
			} catch (java.lang.Throwable t){}
		}
	}
	
	private class CourseListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				course = (Course) coursecomboBox.getSelectedItem();
			
				getClient().sendMsgToServer(new MessageGetLessonByCourse(course.getIdCourse()));
				MessageLessonReplay msg3 = (MessageLessonReplay) getClient().getMessageFromServer();
				lessonArray = msg3.getArray();
			
				for (int i=0; i<=lessonArray.size(); i++){
					if (i==0) lessoncomboBox.addItem("Choose..");
					else lessoncomboBox.addItem(lessonArray.get(i-1));
				}
				coursecomboBox.setEnabled(false);
				lessoncomboBox.setEnabled(true);
				lessoncomboBox.addActionListener(new LessonListener());
			} catch (java.lang.Throwable t){}
		}
	}
	
	private class LessonListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				lesson = (Lesson) lessoncomboBox.getSelectedItem();
				btnAssign.setEnabled(true);		
			} catch (java.lang.Throwable t){}	
		}
	}
	
	public MyJPanel pushPanel() {
		return new AssignStudentToCoursePanel(getClient());
	}
}


